import 'package:flutter/material.dart';
import 'each_screen.dart';

// 定义有状态的widget
class BottomAppBarDemo extends StatefulWidget {
  @override
  _BottomAppBarDemoState createState() {
    return _BottomAppBarDemoState();
  }
}

// 状态管理类
class _BottomAppBarDemoState extends State<BottomAppBarDemo> {
  // 定义appbar的主题色
  final _BottomAppBarColor = Colors.lightBlue;

  // 定义视图List
  List<Widget> screenList = List();

  // 视图索引，默认是第一个页面
  int _currentIndex = 0;

  // 初始化将所有的页面加载到视图List中
  @override
  void initState() {
    super.initState();
    screenList
      ..add(EachScreen("Home"))
      ..add(EachScreen("Email"))
      ..add(EachScreen("Print"))
      ..add(EachScreen("Person"));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: screenList[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
              icon: Icon(
                Icons.home,
                color: _BottomAppBarColor,
              ),
              title: Text(
                'Home',
                style: TextStyle(color: _BottomAppBarColor),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.email,
                color: _BottomAppBarColor,
              ),
              title: Text(
                'Email',
                style: TextStyle(color: _BottomAppBarColor),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.print,
                color: _BottomAppBarColor,
              ),
              title: Text(
                'Print',
                style: TextStyle(color: _BottomAppBarColor),
              )),
          BottomNavigationBarItem(
              icon: Icon(
                Icons.person,
                color: _BottomAppBarColor,
              ),
              title: Text(
                'Person',
                style: TextStyle(color: _BottomAppBarColor),
              ))
        ],
        type: BottomNavigationBarType.fixed,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
      ),
      floatingActionButton: FloatingActionButton(
        // 路由跳转的新页面
        onPressed: () {
          Navigator.of(context)
              .push(MaterialPageRoute(builder: (BuildContext content) {
            return EachScreen("new page");
          }));
        },
        tooltip: 'add',
        child: Icon(
          Icons.add,
          color: Colors.white,
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }
}
